package com.xinghou.platform.dao.main.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.xinghou.platform.dao.bases.JdbcTemplate;
import com.xinghou.platform.dao.bases.database.CustomerContextHolder;
import com.xinghou.platform.dao.main.VisitFromDAO;
import com.xinghou.platform.model.main.VisitFrom;

/**
 * 
 * @author wxtdy
 *
 */
@Service
public class VisitFromDAOImpl  {/*
	@Resource
	private JdbcTemplate jdbcTemplate;

	@Override
	public VisitFrom getVisitFromById(Integer Id)throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT Id, From_user_id, To_user_id, Create_time, Update_time FROM visit_from WHERE Id = ?";
		VisitFrom Visitfrom = jdbcTemplate.queryForObject(sql, new Object[] { Id }, new VisitFromMapper());
		CustomerContextHolder.clearCustomerType();
		return Visitfrom;
	}

	@Override
	public int updateVisitFrom(VisitFrom visitFrom)throws DataAccessException {
		StringBuffer strSQL = new StringBuffer("UPDATE visit_from SET From_user_id=?,To_user_id=?,Create_time=?,Update_time=? WHERE Id=?");
		Object[] params = {visitFrom.getFromUserId(),visitFrom.getToUserId(),visitFrom.getCreateTime(),visitFrom.getUpdateTime(),visitFrom.getId()};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret = jdbcTemplate.update(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}

	@Override
	public int saveVisitFrom(VisitFrom visitFrom) throws DataAccessException {
		StringBuffer strSQL = new StringBuffer("INSERT INTO visit_from (From_user_id,To_user_id,Create_time,Update_time) VALUES (?,?,?,?);");
		Object[] params = {visitFrom.getFromUserId(),visitFrom.getToUserId(),visitFrom.getCreateTime(),visitFrom.getUpdateTime()};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret =jdbcTemplate.save(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}
	
	@Override
	public List<VisitFrom> getVisitFromByFromUserId(Integer fromUserId,Timestamp time,int rowNum) {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT Id, From_user_id, To_user_id, Create_time,Update_time FROM visit_from WHERE From_user_id = ? AND Update_time<? ORDER BY Update_time DESC LIMIT ?";
		List<VisitFrom> list = jdbcTemplate.query(sql, new Object[] { fromUserId,time,rowNum}, new VisitFromMapper());
		CustomerContextHolder.clearCustomerType();
		return list;
	}
	
	private static final class VisitFromMapper implements RowMapper<VisitFrom> {
		public VisitFrom mapRow(ResultSet rs, int rowNum) throws SQLException {
			VisitFrom visitfrom = new VisitFrom();
			visitfrom.setId(rs.getInt("Id"));
			visitfrom.setFromUserId(rs.getInt("From_user_id"));
			visitfrom.setToUserId(rs.getInt("To_user_id"));
			visitfrom.setCreateTime(rs.getTimestamp("Create_time"));
			visitfrom.setUpdateTime(rs.getTimestamp("Update_time"));
			return visitfrom;
		}
	}

	@Override
	public VisitFrom getVisitFromByTwoId(int fromUserId, int toUserId)
			throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT Id, From_user_id, To_user_id, Create_time, Update_time FROM visit_from WHERE From_user_id = ? AND To_user_id=? LIMIT 1";
		VisitFrom Visitfrom = jdbcTemplate.queryForObject(sql, new Object[] { fromUserId, toUserId}, new VisitFromMapper());
		CustomerContextHolder.clearCustomerType();
		return Visitfrom;
	}
	
	@Override
	public List<VisitFrom> getVisitFromGroupByFromUserId(Integer fromUserId,
			Timestamp time, int rowNum) {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT * FROM (SELECT Id, From_user_id, To_user_id, Create_time,Update_time FROM visit_from WHERE From_user_id = ? ORDER BY Update_time DESC) t GROUP BY To_user_id ORDER BY t.Update_time DESC LIMIT ?,20";
		List<VisitFrom> list = jdbcTemplate.query(sql, new Object[] { fromUserId,rowNum}, new VisitFromMapper());
		CustomerContextHolder.clearCustomerType();
		return list;
	}*/

}
